'use strict';
angular.module('stmms')
    .controller('IndexCtrl', ['$scope','$http','$stateParams','Work', function ($scope,$http,$stateParams,Work) {
        var schoolId = 2;
        $scope.works = Work.findBySchool({schoolId:schoolId});
        
    }])
    .controller('PaperCtrl', ['$scope','ngDialog', function ($scope,ngDialog) {
        $scope.pdfUrl = '/slice/93002250A.pdf';

        $scope.questions = [
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0},
           
            {idx:1,score:0},
            {idx:1,score:0},
            {idx:1,score:0}
        ];

        $scope.scores = [2,4,6,8,10,12,14,16,18,20];

        $scope.pop = {
            content: 'Hello, World!',
            templateUrl: 'popover.html',
            title: 'Title'
          };

        $scope.show = function(){
            ngDialog.open({
                template: 'tpl/panel.html',
                className: 'ngdialog-theme-plain',
                scope: $scope,
                cache:false
            });
        }

        $scope.review = function(){
            ngDialog.open({
                template: 'tpl/review.html',
                className: 'ngdialog-theme-plain',
                scope: $scope,
                cache:false
            });
        }
    }])
    .controller('HomeCtrl', ['$scope','$http','$stateParams', function ($scope,$http,$stateParams) {
        
    }])
    .controller('SubjectCtrl', ['$scope','MarkSubject','$stateParams', function ($scope,MarkSubject,$stateParams) {
        var workId = $stateParams.workId;
        $scope.subjects = MarkSubject.findByWork({workId:workId});
    }])
    .controller('MarkerCtrl', ['$scope','Marker','$stateParams', function ($scope,Marker,$stateParams) {
        var workId = $stateParams.workId;
        $scope.markers = Marker.findByWork({workId:workId});
    }])
    .controller('MarkerDetailCtrl', ['$scope','Marker','MarkerTask','$stateParams', function ($scope,Marker,MarkerTask,$stateParams) {
        var markerId = $stateParams.markerId;
        var marker = $scope.marker = Marker.get({markerId:markerId},function(){
            $scope.tasks = marker.tasks;
        });
        
        $scope.options = {
            animate: false,
            barColor: '#019fbb',
            scaleColor: false,
            lineWidth: 10,
            lineCap: 'butt'
        };

        $scope.removeTasks = function(task){
            if(task.finished){
                alert('已完成的任务不能删除');
                return ;
            }
            var c = window.confirm('是否要删除任务？')
            if(c){
                $scope.tasks = MarkerTask.removeTasks({markerId:markerId,taskId:task.id},function(){
                    $scope.marker = Marker.get({markerId:markerId});
                })
            }
            
        }
       
    }])
    .controller('MarkerTasksCtrl', ['$scope','Marker','MarkerTask','MarkSubject','$stateParams','$state', function ($scope,Marker,MarkerTask,MarkSubject,$stateParams,$state) {
        var workId = $stateParams.workId;
        var markerId = $stateParams.markerId;

        $scope.marker = Marker.get({markerId:markerId});
        $scope.subjects = MarkSubject.findByWork({workId:workId,unassigned:true},function(){
            $scope.subjects.forEach(function(item){
                item.selected = false;
                
            });
        });

        $scope.select = function(subject){
            subject.selected?subject.selected = false:subject.selected = true;
        }

        $scope.submit = function(){
            var selects = _.where($scope.subjects,{selected:true});
            var taskId = [];
            selects.forEach(function(item){
                taskId.push(item.id);
            })
            MarkerTask.createTasks({markerId:markerId},taskId,function(){
                $state.go('mark.markerdetail',{markerId:markerId});
            });
        }
    }])
    .controller('TaskPapersCtrl',['$scope','Marker','Task','Paper','$stateParams',function($scope,Marker,Task,Paper,$stateParams){
        var taskId = $stateParams.taskId;
        var markerId = $stateParams.markerId;
        $scope.marker = Marker.get({markerId:markerId});
        $scope.task = Task.get({taskId:taskId});
        $scope.papers = Paper.findMarkedPapers({taskId:taskId});
    }])
    
    